package com.gengzp.mq.server.facade.impl;

import com.gengzp.mq.common.model.entity.GengzpMqMessage;
import com.gengzp.mq.common.utils.GengzpMqAssertUtils;
import com.gengzp.mq.server.facade.IGengzpMqFacadeService;
import com.gengzp.mq.server.mapper.GengzpMqMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName GengzpMqFacadeServiceImpl
 * @Description 消息队列模块对外接口实现类
 * @Author gengzp
 * @Date 2025/6/8 20:26
 */
@Service
public class GengzpMqFacadeServiceImpl implements IGengzpMqFacadeService {

    @Autowired
    private GengzpMqMessageMapper gengzpMqMessageMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GengzpMqMessage> searchNeedRetryMessage(List<Long> ids) {
        return gengzpMqMessageMapper.selectNeedRetryMessage(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchEditMessageStatus(List<Long> messageIds, Integer status) {
        GengzpMqAssertUtils.isNotEmpty(messageIds, "messageIds不能为空");
        gengzpMqMessageMapper.batchUpdateMessageStatus(messageIds, status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchRetryMessages(List<Long> messageIds) {
        GengzpMqAssertUtils.isNotEmpty(messageIds, "messageIds不能为空");
        gengzpMqMessageMapper.batchRetryMessages(messageIds, new Date());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GengzpMqMessage> searchMessageByStatus(Integer status) {
        GengzpMqAssertUtils.nonNull(status, "status不能为空");
        return gengzpMqMessageMapper.selectMessageByStatus(status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteMessage(List<Long> messageIds) {
        gengzpMqMessageMapper.batchDeleteMessageByIds(messageIds);
    }

    @Override
    public List<GengzpMqMessage> searchMessageByTopics(List<String> topics) {
        return null;
    }

    @Override
    public List<GengzpMqMessage> searchMessageByCreatedTime(Date createdTimeStart,
                                                            Date createdTimeEnd) {
        if (Objects.isNull(createdTimeStart) || Objects.isNull(createdTimeEnd)) {
            return Collections.emptyList();
        }

        return gengzpMqMessageMapper.selectMessageByCreatedTime(createdTimeStart, createdTimeEnd);
    }

    @Override
    public List<GengzpMqMessage> searchTimeoutMessageByCreatedTime(Date createdTimeEnd) {
        if (Objects.isNull(createdTimeEnd)) {
            return Collections.emptyList();
        }
        return gengzpMqMessageMapper.selectTimeoutMessageByCreatedTime(createdTimeEnd);
    }

    @Override
    public List<GengzpMqMessage> searchDeathMessageByCreatedTime(Date createdTimeStart,
                                                                 Date createdTimeEnd) {
        if (Objects.isNull(createdTimeStart) || Objects.isNull(createdTimeEnd)) {
            return Collections.emptyList();
        }

        return gengzpMqMessageMapper.selectDeathMessageByCreatedTime(createdTimeStart,
                createdTimeEnd);
    }

}
